package com.biwu;

import cn.hutool.json.JSONUtil;

import java.lang.reflect.Method;

/**
 * @author 王俊彪
 * @version 1.0
 */
public class ResultUtils {

    public static <T> String result(Enum anEnum,T data){
        ReposeData<T> reposeData = new ReposeData<>();
        reposeData.setData(data);
        try {
            if(anEnum != null) {
                Method codeMethod = anEnum.getClass().getMethod("getCode");
                Method megMethod = anEnum.getClass().getMethod("getMeg");
                if(codeMethod!=null){
                    reposeData.setCode((Integer) codeMethod.invoke(anEnum));
                }
                if(megMethod!=null){
                    reposeData.setMeg((String) megMethod.invoke(anEnum));
                }

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return JSONUtil.toJsonStr(reposeData);
    }
}
